package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.CodeBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.MyConst;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ForgetPsdActivity extends BaseActivity
{
    @Bind(R.id.et_forgetpsd_code)
    EditText etCode;
    @Bind(R.id.et_forgetpsd_phone)
    EditText etPhone;
    @Bind(R.id.btn_forgetpsd_getcode)
    Button btnGetCode;
    int delaytime;
    @Override
    protected int contentView()
    {
        return R.layout.activity_forget_psd;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.forgetpsd);
        mToolbar.inflateMenu(R.menu.menu_next);
        initEvent();
    }

    @Override
    protected void initEvent() {
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
        {
            @Override
            public boolean onMenuItemClick(MenuItem item)
            {
                //重置密码
                if(TextUtils.isEmpty(etPhone.getText().toString()))
                {
                    showToast(R.string.phonenotnull);
                }
                else if(etPhone.getText().toString().length()!=11)
                {
                    showToast(R.string.phonefalse);
                } else if (TextUtils.isEmpty(etCode.getText().toString()))
                {
                    showToast(R.string.codenotnull);

                }else if (etCode.getText().toString().equals(MyConst.code))
                {
                    Intent intent=new Intent(ForgetPsdActivity.this, SetNewPsdActivity.class);
                    intent.putExtra("phone",etPhone.getText().toString());
                    startActivity(intent);
                    finish();
                }else  if (!etCode.getText().toString().equals(MyConst.code))
                {
                    showToast(R.string.codefalse);
                }
                return false;
            }
        });

        btnGetCode.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                getCode();
            }
        });
    }

    private void getCode()
    {
        if(TextUtils.isEmpty(etPhone.getText().toString()))
        {
            showToast(R.string.phonenotnull);
        }
        else if(etPhone.getText().toString().length()!=11)
        {
            showToast(R.string.phonefalse);
        }else
        {

            getTestApi().getCode(etPhone.getText().toString())
                    .compose(this.<BaseBean>bindToLifecycle())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<BaseBean>()
                    {
                        @Override
                        public void onCompleted()
                        {

                        }

                        @Override
                        public void onError(Throwable e)
                        {
                            showToast(R.string.neterror);
                        }

                        @Override
                        public void onNext(BaseBean baseBean)
                        {
                            if(baseBean.getReturnNo().equals("1"))
                            {
                                String result= AESUtils.decode(baseBean.getContent());
                                result=start+result+end;
                                Log.e("result", result);
                                CodeBean bean=new Gson().fromJson(result,CodeBean.class);
                                MyConst.code=bean.getContent()+"";
                                Toast.makeText(ForgetPsdActivity.this, R.string.codeisout, Toast.LENGTH_SHORT).show();
                                delaytime = 60;
                                btnGetCode.setClickable(false);
                                new Thread(new ThreadShow()).start();
                            }else
                            {
                                showToast(R.string.codegetfalse);
                            }
                        }
                    });
        }
    }
    // 线程类
    class ThreadShow implements Runnable
    {
        @Override
        public void run()
        {
            while (delaytime > 0)
            {
                try
                {
                    Thread.sleep(1000);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
                delaytime--;
                Message msg = new Message();
                msg.what = 88;
                handler.sendMessage(msg);

            }
        }
    }
    Handler handler=new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            switch (msg.what)
            {
                case 88:
                    btnGetCode.setText(delaytime + "s");
                    if (delaytime == 0)
                    {
                        btnGetCode.setClickable(true);
                        btnGetCode.setText("发送");
                    }
                    break;
            }
        }
    };

}
